<template>
  <ul class="app-header-nav">
    <li class="home">
      <RouterLink to="/home" @click="reloadApp({})">首页</RouterLink>
    </li>
    <li
      v-for="item in list"
      :key="item.id"
      @click="SorH(item, false);reloadApp(item)"
      @mousemove="SorH(item, true)"
      @mouseleave="SorH(item, false)"
      :class="{ active: item.id === activeItem.id }"
    >
      <router-link :to="`/category/${item.id}`">{{ item.name }}</router-link>
      <div class="layer" :class="{ show: item.open }">
        <ul>
          <li v-for="i in item.children" :key="i.id">
            <router-link
              :to="`/category/sub/${i.id}`"
              @click.stop="SorH(item, false);reloadApp(item,i)"
            >
              <img :src="i.picture" alt="" />
              <p>{{ i.name }}</p>
            </router-link>
          </li>
        </ul>
      </div>
    </li>
  </ul>
</template>

<script>
import { computed, inject } from "vue";
import { useStore } from "vuex";
export default {
  name: "AppHeaderNav",
  setup() {
    const STORE = useStore();

    const list = computed(() => {
      return STORE.state.category.categoryList;
    });
    // JS替hover
    const SorH = (item, flag) => {
      STORE.commit("category/showOrhide", { item, flag });
    };
    // 当前激活项
    const activeItem = computed(() => {
      return STORE.state.category.activeItem
    });

    const setActive = (item) => {
      STORE.commit('category/setActiveItem', item)
    }


    // 刷新/重载页面
    const reload = inject('reloadApp')
    const reloadApp = (item, child = {}) => {
      if (child.id) {
        if (child.id === activeItem.value.activeChild.id) {
          console.log('reload');
          reload()
        }
      } else {
        if (item.id === activeItem.value.id) {
          console.log('reload');
          reload()
        }
      }
      // console.log('setActive');
      setActive({ ...item, activeChild: child })
    }


    return { list, SorH, activeItem, reloadApp };
  }
};
</script>

<style scoped lang="less">
.app-header-nav {
  width: 820px;
  display: flex;
  justify-content: space-around;
  padding-left: 40px;
  position: relative;
  z-index: 998;
  .active {
    & > a:first-child {
      color: @xtxColor;
      font-size: 18px;
      font-weight: bold;
      border-bottom: 2px solid @xtxColor;
    }
  }
  > li {
    margin-right: 40px;
    width: 38px;
    text-align: center;
    > a {
      font-size: 16px;
      line-height: 32px;
      height: 32px;
      display: inline-block;
    }
    &:hover {
      > a {
        color: @xtxColor;
        border-bottom: 1px solid @xtxColor;
      }
    }
  }
}

// 子分类
.layer {
  &.show {
    height: 132px;
    opacity: 1;
  }
  width: 1240px;
  background-color: #fff;
  position: absolute;
  left: -200px;
  top: 56px;
  height: 0;
  overflow: hidden;
  opacity: 0;
  box-shadow: 0 0 5px #ccc;
  transition: all 0.2s 0.1s;
  ul {
    display: flex;
    flex-wrap: wrap;
    padding: 0 70px;
    align-items: center;
    height: 132px;
    li {
      width: 110px;
      text-align: center;
      img {
        width: 60px;
        height: 60px;
      }
      p {
        padding-top: 10px;
      }
      &:hover {
        p {
          color: @xtxColor;
        }
      }
    }
  }
}
</style>
